﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NOMFinalSettlement.Classes
{
    class BackupDatabase
    {
        public string BackUpDir = null;
        public bool BackUpAuto = false;
        public bool BackUpRemote = false;


        public void LoadSetting()
        {
            FinalSettlementModel.Properties.Settings.Default.Reload();
            BackUpDir = FinalSettlementModel.Properties.Settings.Default.BackUpDir;
            BackUpAuto = FinalSettlementModel.Properties.Settings.Default.BackUpAuto;
            BackUpRemote = FinalSettlementModel.Properties.Settings.Default.BackUpRemote;
        }

        public void SaveSetting()
        {
            if (string.IsNullOrEmpty(BackUpDir)) return;
            FinalSettlementModel.Properties.Settings.Default.BackUpDir = BackUpDir;
            FinalSettlementModel.Properties.Settings.Default.BackUpAuto = BackUpAuto;
            FinalSettlementModel.Properties.Settings.Default.BackUpRemote = BackUpRemote;

            FinalSettlementModel.Properties.Settings.Default.Save();
        }

        public void ListBackupFile(DevComponents.DotNetBar.Controls.ListViewEx listViewFolder)
        {
            if (string.IsNullOrEmpty( BackUpDir )) return;
            listViewFolder.Items.Clear();
            try
            {
                var fList = Directory.GetFiles(@"" + BackUpDir + "");
                var sort = from s in fList
                           orderby s descending
                           select s;
                foreach (string _filePath in sort)
                {
                    if (__IsBak(_filePath))
                    {
                        var fileName = _filePath.Replace(BackUpDir + "\\", "");
                        listViewFolder.Items.Add(fileName).SubItems.Add(fileName);
                    }
                }
            }
            catch { }

        }
        private bool __IsBak(string filePath)
        {
            return filePath != null && filePath.EndsWith(".bak", StringComparison.Ordinal);
        }

        public bool DeleteBackup(string f)
        {
            try
            {
                File.Delete(f);
                return true;
            }
            catch (IOException)
            {
                return false;
            }
        }
        public void DeleteAllBackUp()
        {
            try
            {
                var fList = Directory.GetFiles(@"" + BackUpDir + "");
                var sort = from s in fList
                           orderby s descending
                           select s;
                foreach (string _filePath in sort)
                {
                    if (__IsBak(_filePath))
                    {
                        this.DeleteBackup(_filePath);
                    }
                }
            }
            catch { }
        }
        public string GetItemListView(DevComponents.DotNetBar.Controls.ListViewEx list)
        {
            int countList = list.Items.Count;
            var item = "";
            for (int i = 0; i < countList; i++)
            {
                if (list.Items[i].Selected)
                {
                    item = BackUpDir + "\\" + list.Items[i].SubItems[0].Text;
                    break;
                }

            }
            return item;
        }
    }
}
